puppetdir='/etc/puppet'
setupdir='/propuppet_data'

function install_package(){
	if [ ! -f /usr/bin/puppet ]; then
		echo "installing $1"
		sudo yum install $1 -y
	else
		echo "$1 already installed"
	fi
	
}

if [ ! -f /etc/yum.repos.d/puppetlabs.repo ]; then
	echo "Adding puppet repo"
	sudo rpm -ivh https://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-7.noarch.rpm
	echo "192.168.33.11 puppet3-server.example.com puppet" >> /etc/hosts
	echo "192.168.33.12 puppet3-agent.example.com" >> /etc/hosts
else
	echo "Repo already installed"
fi

if `hostname | grep -q 'server'`; then 
	install_package puppet-server
	if [ -f "$puppetdir/environments/test" ]; then
		echo "Environment test already exists"
	else
		yes | cp -rf $setupdir/puppet.conf $puppetdir/puppet.conf
		yum -y install PyYAML
		cp $setupdir/simple_notifier /usr/local/bin/
		mkdir -p "$puppetdir/environments/test"
		for i in manifests modules; do 
			if [ -L "$puppetdir/environments/test/$i" ]; then
				echo "$puppetdir/environments/test/$i symlink already exists"	
			else
				#echo "removing $puppetdir/$i"
				#rm -Rf "$puppetdir/environments/test/$i"
				echo "Creating symlink"
				ln -s $setupdir/$i "$puppetdir/environments/test/$i"
			fi
		done
		/etc/init.d/puppetmaster start
	fi
else
	install_package puppet
fi
